<?php

namespace App\Observers;

use App\Models\Device;
use App\Models\SmsLog;
use App\Models\VisitLog;
use Carbon\Carbon;
use Illuminate\Support\Facades\Log;

class SmsLogObserver
{
    /**
     * Handle the SmsLog "created" event.
     *
     * @param \App\Models\SmsLog $model
     * @return void
     */
    public function created(SmsLog $model)
    {
        $device = Device::query()->where(['device_id' => $model->device_id])->first();
        $device->sms_at = Carbon::now();
        $device->save();
    }

    /**
     * Handle the SmsLog "updated" event.
     *
     * @param \App\Models\SmsLog $model
     * @return void
     */
    public function updated(SmsLog $model)
    {
        //
    }

    /**
     * Handle the SmsLog "deleted" event.
     *
     * @param \App\Models\SmsLog $model
     * @return void
     */
    public function deleted(SmsLog $model)
    {
        //
    }

    /**
     * Handle the SmsLog "restored" event.
     *
     * @param \App\Models\SmsLog $model
     * @return void
     */
    public function restored(SmsLog $model)
    {
        //
    }

    /**
     * Handle the SmsLog "force deleted" event.
     *
     * @param \App\Models\SmsLog $model
     * @return void
     */
    public function forceDeleted(SmsLog $model)
    {
        //
    }

    public function creating(SmsLog $model)
    {

    }

    public function saving(SmsLog $model)
    {
        $device = Device::query()->where(['device_id' => $model->device_id])->first();
        $model->device_code = $device->code;

    }
    public function saved(SmsLog $model){
        if($model->device){
            $model->device->count_sms = SmsLog::query()->where('device_id',$model->device_id)->count();
            $model->device->save();
        }
        if($model->phone_number_model){
            $model->phone_number_model->sms_at=Carbon::now();
            $model->phone_number_model->count_sms = SmsLog::query()->where('sim',$model->sim)->count();
            $model->phone_number_model->save();
        }


    }
}
